package ru.jarda.model.dao;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.jarda.model.entities.Account;

import java.util.List;


/**
 * Created by user on 18.03.2015.
 */
@Component
@Transactional
public class AccountDaoImpl implements Dao<Account> {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public Account getObject(Number id) {
        Session session = sessionFactory.getCurrentSession();
       return (Account)session.get(Account.class,id);
    }

    @Override
    public void deleteObject(Number id) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(new Account((long)id));
    }


    @Override
    public void saveObject(Account account) {
       Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(account);
    }

    @Override
    public List<Account> getAllObjects() {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria=session.createCriteria(Account.class);

        criteria.add( Restrictions.eq("userId", new Account().getUserId()));
        return criteria.list();
    }

    @Override
    public void editObject(Account entity) {

    }

}
